home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n04 / handprin.urc / Recognizer / FileIO.c / FileIO.c
Encoding:
C/C++ Source or Header  |  1992-03-05  |  4.1 KB  |  181 lines  |  [TEXT/KAHL]

  1. /* All rights reserved. Copyright Ron Avitzur - 1992
  2. */
  3.  
  4. #include "Recognizer.h"
  5.  
  6. extern List patterns;
  7.  
  8. void WriteData(FILE *file);
  9. void WriteGesturePattern(FILE *file,GesturePattern *gp);
  10. void WriteStrokePattern(FILE *file,StrokePattern *sp);
  11. void ReadData(FILE *file);
  12. void ReadGesturePattern(FILE *file,GesturePattern *gp);
  13. void ReadStrokePattern(FILE *file,StrokePattern *sp);
  14. void ConstructStr(Handle h,List s,short bits,char *name);
  15. void InterpretStr(char *str,List *s,short bits,char *name);
  16.  
  17. short GetToken(char *token,char **sp);
  18. short GetToken(char *token,char **sp)
  19.     {
  20.     short i = 0;
  21.     while (*sp && **sp == ' ') (*sp)++;
  22.     while (*sp && **sp != ' ' && **sp != '\n') {
  23.         token[i++] = **sp;
  24.         (*sp)++;
  25.         }
  26.     token[i] = 0;
  27.     return i != 0;
  28.     }
  29.  
  30. void InterpretStr(char *str,List *s,short bits,char *name)
  31.     {
  32.     char token[30];
  33.     unsigned long n;
  34.     short i;
  35.  
  36.     if (name) for (i = 0; i < strlen(str); i++) {
  37.         if            (str[i] == name[0]) str[i] = '0';
  38.         else if    (str[i] == name[1]) str[i] = '1';
  39.         else if    (str[i] == name[2]) str[i] = '2';
  40.         else if    (str[i] == name[3]) str[i] = '3';
  41.         }
  42.     while (GetToken(token,&str)) {
  43.         ConvertStringToLong(token,&n,bits);
  44.         Append(s,n);
  45.         }
  46.     }
  47.  
  48. void ConstructStr(h,s,bits,name)
  49.  Handle h;
  50.  List s;
  51.  short bits;
  52.  char *name;
  53.     {
  54.     char str[255];
  55.     short i,j,len;
  56.  
  57.     HUnlock(h);
  58.     SetHandleSize(h,0);
  59.     if (s == NIL) return;
  60.     for (i = 0; i < s->num_items; i++) {
  61.         ConvertLongToString(str,(long)s->items[i],bits,name);
  62.         len = GetHandleSize(h);
  63.         SetHandleSize(h,len + strlen(str) + 1);
  64.         for (j = len; j < len + strlen(str); j++)
  65.             (*h)[j] = str[j - len];
  66.         (*h)[j] = ' ';
  67.         }
  68.     (*h)[j] = 0;
  69.     HLock(h);
  70.     }
  71.  
  72. void ReadPatterns()
  73.     {
  74.     FILE *file;
  75.     file = fopen("rxa.dat","r");
  76.     ReadData(file);
  77.     fclose(file);
  78.     MakeSearchTables();
  79.     }
  80.  
  81. void SavePatterns()
  82.     {
  83.     FILE *file;
  84.     file = fopen("rxa.dat","w");
  85.     WriteData(file);
  86.     fclose(file);
  87.     }
  88.  
  89. void ReadData(file)
  90.  FILE *file;
  91.     {
  92.     short i,num_patterns;
  93.     fscanf(file,"%d Patterns\n\n",&num_patterns);
  94.     if (num_patterns == 0) {
  95.         Append(&patterns,0L);
  96.         patterns->num_items = 0;
  97.         }
  98.     for (i = 0; i < num_patterns; i++)
  99.         {
  100.         GesturePattern *gp = malloc(sizeof(GesturePattern));
  101.         if (gp == NIL) {SysBeep(1); Debugger(); ExitToShell();}
  102.         gp->strokes = NIL;
  103.         ReadGesturePattern(file,gp);
  104.         Append(&patterns,(long)gp);
  105.         }
  106.     }
  107.  
  108. void WriteData(file)
  109.  FILE *file;
  110.     {
  111.     short i;
  112.     fprintf(file,"%d Patterns\n\n",patterns->num_items);
  113.     for (i = 0; i < patterns->num_items; i++)
  114.         WriteGesturePattern(file,patterns->items[i]);
  115.     }
  116.  
  117. void ReadGesturePattern(FILE *file,GesturePattern *gp)
  118.     {
  119.     short i,j,num_strokes; char c; char str[255];
  120.     fscanf(file,"%s\n",str);
  121.     if (strlen(str) == 1)
  122.             gp->code = str[0];
  123.     else    sscanf(str,"%x",&gp->code);
  124.  
  125.     fscanf(file," %d\n",&num_strokes);
  126.     for (i = 0; i < num_strokes; i++)
  127.         {
  128.         StrokePattern  *sp = malloc(sizeof(StrokePattern));
  129.         if (sp == NIL) {SysBeep(1); Debugger(); ExitToShell();}
  130.         sp->is_dot = 0;
  131.         for (j=0;j<NUM_FEATURES;j++) sp->s[j] = NIL;
  132.         ReadStrokePattern(file,sp);
  133.         Append(&gp->strokes,(long)sp);
  134.         }
  135.     fscanf(file,"\n");
  136.     }
  137.  
  138. void WriteGesturePattern(FILE *file,GesturePattern *gp)
  139.     {
  140.     short i;
  141.  
  142.     if (gp->code <= 0x7E && gp->code >= 0x21)    fprintf(file, "%c\n",gp->code);
  143.     else if (gp->code <= 0xf)                        fprintf(file,"0%x\n",gp->code);
  144.     else                                                    fprintf(file, "%x\n",gp->code);
  145.  
  146.     fprintf(file," %d\n",gp->strokes->num_items);
  147.     for (i = 0; i < gp->strokes->num_items; i++)
  148.         WriteStrokePattern(file,gp->strokes->items[i]);
  149.     fprintf(file,"\n");
  150.     }
  151.  
  152. void ReadStrokePattern(FILE *file,StrokePattern *sp)
  153.     {
  154.     char *p,type,str[1000],i;
  155.     
  156.     while (TRUE) {
  157.         fgets(str,1000,file);
  158.         if (str[0] == '\n')    return;
  159.         if (str[0] == '\t')    { type = str[1]; p = &str[3]; }
  160.         else                        { type = str[0]; p = &str[2]; }
  161.         if (type == 'a')        sp->is_dot = 1;
  162.         else if ((i = type - 'b') < NUM_FEATURES)
  163.             InterpretStr(p,&sp->s[i],Bits[i],Names[i]);
  164.         }
  165.     }
  166.  
  167.  
  168. void WriteStrokePattern(FILE *file,StrokePattern *sp)
  169.     {
  170.     short i;
  171.     Handle h = NewHandle(0);
  172.     if (sp->is_dot) fprintf(file,"\ta\n");
  173.     for (i=0;i<NUM_FEATURES;i++) if (sp->s[i]) {
  174.         ConstructStr(h,sp->s[i],Bits[i],Names[i]);
  175.         fprintf(file,"\t%c: %s\n",(char)('b' + i),*h);
  176.         }
  177.     fprintf(file,"\n");
  178.     DisposHandle(h);
  179.     }
  180.  
  181.